package xianchenglock;

import java.util.concurrent.TimeUnit;

/**
 * @功能
 * @创建时间 2019/12/17
 */
public class deadlock {
    private int count = 10;
    private Object o = new Object();
    private Object p = new Object();
    public void m1(){
        synchronized (o){ //任何线程要执行必须拿到o的锁
            count++;

            System.out.println("1");
            synchronized (p){ //任何线程要执行必须拿到o的锁
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2");
            }
        }

    }
    public void m2(){
        synchronized (p){ //任何线程要执行必须拿到o的锁
            count++;

            System.out.println("3");
            synchronized (o){ //任何线程要执行必须拿到o的锁
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("4");
            }
        }

    }
    public static void main(String[] args) {
        deadlock t = new deadlock();
        for (int i=0;i<2;i++){
        new Thread(()->t.m1(),"t1").start();
        new Thread(()->t.m2(),"t2").start();}
    }

}
